<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+


class KBClientAction_contact extends KBClientAction_common
{

	function &execute($controller, $manager) {
		
		// not allowed
		if(!$manager->getSetting('allow_contact')) {
			$controller->go();
		}
		
		// need to login
		if(!$manager->is_registered && $manager->getSetting('allow_contact') == 2) { 
			$controller->go('login', $this->category_id, $this->entry_id, 'contact'); 
		}
	
		// attachment
		if($manager->getSetting('contact_attachment')) {
			$file_settings = &SettingModel::getQuick('200');
			$this->attachment_dir = $file_settings['file_dir'] . 'contact_attachment/';
			$this->attachment_max_filesize = ini_get('upload_max_filesize')*1024;
			//$this->attachment_max_filesize = 15;
		}
		

		$view = &$controller->getView();
				
		if(isset($this->rp->submit)) {
			
			$errors = $view->validate($this->rp->vars, $manager);
			//$flood = $manager->isFlood();
				
			if($errors) {
			
				$this->rp->stripVars(true);
				$view->setErrors($errors);
				$view->setFormData($this->rp->vars);
			
			//} elseif($flood) {
			
			//	$rp->stripVars(true);
			//	$view->setFormData($rp->vars);
			//	$view->msg_id = 'flood_comment';
			
			} else {
			
				// with file
				$files = array();
				if(!empty($_FILES['file_1']['name']) && $manager->getSetting('contact_attachment')) {
					
					$upload = &$this->upload();
					//echo "<pre>"; print_r($upload); echo "</pre>";
					//exit;
	
					if(!empty($upload['error_msg'])) {
						$errors['formatted'] = $upload['error_msg'];
						$view->setErrors($errors);
						$view->setFormData($this->rp->vars);
					
					} else {
						foreach($upload['good'] as $k => $v) {
							$files[] = $this->attachment_dir . $v['name'];
						}
					}
				}
				
				
				if(!$errors) {
				
					$this->rp->stripVars('stripslashes');
					$this->rp->vars['attachment'] = $files;
					$sent = $manager->sendContactNotification($this->rp->vars, $view);
					
					if($sent) {
						$this->rp->stripVars('addslashes');
						$this->rp->vars['attachment'] = implode(';', $files);
						$manager->addContactMessage($this->rp->vars);	
						$controller->go('success_go', $this->category_id, $this->entry_id, 'entry_sent');
					
					} else {
						$this->removeUploaded($files);
						
						$this->rp->stripVars(true);
						$view->setFormData($this->rp->vars);
						$view->msg_id = 'entry_not_sent';
					}
				}		
			}
		}
		
		return $view;
	}
	
	
	function upload() {
			
		require_once 'eleontev/Dir/Uploader.php';
	
		$upload = new Uploader;
		$upload->store_in_db = false;
		$upload->safe_name = true;
		$upload->setRenameValues('date');
		//$upload->setAllowedExtension($this->setting['file_allowed_extensions']);
		//$upload->setDeniedExtension($this->setting['file_denied_extensions']);
		$upload->setMaxSize($this->attachment_max_filesize);
		$upload->setUploadedDir($this->attachment_dir);
		
		$f = $upload->upload($_FILES);
	
		if(isset($f['bad'])) {
			$f['error_msg'] = $upload->errorBox($f['bad']);
		}

		return $f;
	}
	
	
	function removeUploaded($files) {
		foreach($files as $file) {
			unlink($file);
		}
	}
}


/*
Array
(
    [good] => Array
        (
            [1] => Array
                (
                    [name] => P1030080_s.jpg
                    [type] => image/jpeg
                    [tmp_name] => C:\DOCUME~1\APACHE~2\LOCALS~1\Temp\php371.tmp
                    [error] => 0
                    [size] => 0
                    [extension] => jpg
                )

        )

)
*/
?>